﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace YJKCoordinate
{
	public class SingleChamferCornerCurveMaker : IMakeCornerCurves, ICheckAngle
	{
		private SingleChamferCornerCurveMaker()
		{
		}

		public static SingleChamferCornerCurveMaker GetSingleChamferCornerCurveMaker()
		{
			if (SingleChamferCornerCurveMaker.singleTag == null)
			{
				SingleChamferCornerCurveMaker.singleTag = new SingleChamferCornerCurveMaker();
			}
			return SingleChamferCornerCurveMaker.singleTag;
		}

		public List<Curve> GetCornerBoundryCurves(ThreeInputDataPacker input, out XYZ startPoint_Mid, out XYZ endPoint_Mind)
		{
			List<Curve> list = new List<Curve>();
			List<Curve> list2;
			List<Curve> list3;
			Utiltiy.GetInnerIntersectionPoint(input.GetTwoInputData(0), input.GetTwoInputData(1), out list2, out list3);
			XYZ xyz;
			XYZ item;
			XYZ xyzpoint;
			XYZ xyzpoint2;
			XYZ xyz2;
			XYZ xyz3;
			if (!Utiltiy.GetCornernRectangle(input, out xyz, out item, out xyzpoint, out xyzpoint2))
			{
				xyz2 = xyz.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(0)).Multiply(input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Distance]));
				xyz3 = xyz.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(1)).Multiply(input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Distance]));
				xyzpoint = list2[1].Project(xyz2).XYZPoint;
				xyzpoint2 = list3[1].Project(xyz3).XYZPoint;
			}
			else if (input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Width] > input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Width])
			{
				xyz2 = xyzpoint.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(0)).Multiply(input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Distance]));
				xyzpoint = list2[1].Project(xyz2).XYZPoint;
				xyz3 = xyz.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(1)).Multiply(input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Distance]));
				xyzpoint2 = list3[1].Project(xyz3).XYZPoint;
			}
			else
			{
				if (input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Width] >= input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Width])
				{
					throw new CornerMakerException(StaticRescource.StrDontWantIf);
				}
				xyz3 = xyzpoint2.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(1)).Multiply(input.GetTwoInputData(1).GetParameters(1)[UseParameterEnum.Distance]));
				xyzpoint2 = list3[1].Project(xyz3).XYZPoint;
				xyz2 = xyz.Add(Utiltiy.GetAVectorAnotherXY(input.GetDriection(0)).Multiply(input.GetTwoInputData(0).GetParameters(1)[UseParameterEnum.Distance]));
				xyzpoint = list2[1].Project(xyz2).XYZPoint;
			}
			startPoint_Mid = xyz2.Add(xyzpoint).Divide(2.0);
			endPoint_Mind = xyz3.Add(xyzpoint2).Divide(2.0);
			list = Utiltiy.GetLstBoundLineCurve(new List<XYZ>
			{
				xyz2,
				xyzpoint,
				item,
				xyzpoint2,
				xyz3
			});
			if (Utiltiy.CheckNoneEndInterintersection(list))
			{
				throw new CornerMakerException(StaticRescource.StrError_cornerLineIntersection);
			}
			return list;
		}

		public bool IfAngleCanUse(ThreeInputDataPacker input)
		{
			throw new NotImplementedException();
		}

		private static SingleChamferCornerCurveMaker singleTag;
	}
}
